package UILayer;

import java.util.Scanner;

/**
 * The InputDialog class is responsible for print messages for the user and read the users input.
 * The MenuClass uses the print and read methods of the InputDialog class and is responsible to 
 * display the messages.
 * 
 * @author anna
 *
 */
public class InputDialog {
    private Scanner scanner;
    
    /**
     * Constructor for InputDialog objects.
     */
    public InputDialog() {
    scanner = new Scanner(System.in);
    }
    
    /**
     * Method for displaying messages for the user. 
     * @param message A text message that can be parametrized in the method call from the Menu class.
     */
    public void displayMessage(String message){
        System.out.println(message);
    }
    
    /**
     * Returns the users input of the type String.
     * 
     * @param prompt A text to by displayed for the user that can be parametrized in the method call
     * from the Menu class.
     * @return res The users input.
     */
    public String inputString(String prompt){
        System.out.print(prompt);
        String res = scanner.nextLine();
        return res;
    }
    
    /**
     * Returns the users input of the type int.
     * 
     * @param prompt A text to by displayed for the user that can be parametrized in the method call
     * from the Menu class.
     * @return res The users input.
     */
    public int inputInt(String prompt){
        String input = inputString(prompt);
        int res = 0;
        try {
            res = Integer.parseInt(input);
        }
        catch (NumberFormatException nfe){
            displayMessage("'" + input + "' er ikke et valid nummer. Prøv igen!");
            res = inputInt(prompt);//recursive call
        }
        return res;
    }
    
    /**
     * Returns the users input of the type double.
     * 
     * @param prompt A text to by displayed for the user that can be parametrized in the method call
     * from the Menu class.
     * @return res The users input.
     */
    public double inputDouble(String prompt){
        String input = inputString(prompt);
        double res = 0;
        try{
            res = Double.parseDouble(input);
        }
        catch (NumberFormatException nfe){
            displayMessage("'" + input + "' er ikke et valid nummer. Prøv igen!");
            res = inputDouble(prompt);
        }
        return res;
    }
    
    /**
     * Returns the users input of the type boolean. 
     * 
     * @param prompt A text to by displayed for the user that can be parametrized in the method call
     * from the Menu class.
     * @return true If the users input is Ja, ja, J or enter.
     * @return false If the users input is Nej, nej, N or n.
     */
    public boolean inputBoolean(String prompt){
        String input = inputString(prompt + " (Ja/nej): ");
        if (input.trim().equalsIgnoreCase("ja") || input.trim().equalsIgnoreCase("j") || input.equals("")){
            return true;
        }
        else if (input.trim().equalsIgnoreCase("nej") || input.trim().equalsIgnoreCase("n")){
            return false;
        }
        displayMessage("Din input var ikke ja eller nej. Du skal taste ja, Ja, j, trykke enter eller taste nej, Nej eller n.");
        return inputBoolean(prompt); //recursive method call
    }
           
    /**
     * 
     */
    public void inputWait(){
        System.out.println();
        System.out.println("Tryk på en vilkårlig tast for at fortsætte.");
        scanner.nextLine();
    }
    
    /**
     * Returns the users input of the type String in the case of update.
     * 
     * @param prompt A text to be displayed for the user that can be parametrized in the method call
     * from the Menu class.
     * @param defaultValue The old value of the String.
     * 
     * @return defaultValue If the user does not want to change the old value 
     * OR
     * inputString If the user wants to change the old value.
     */
    public String updateString(String prompt, String defaultValue){
        if (inputBoolean("Vil du ændre " + prompt + "?")){
            return inputString("Ny værdi for " + prompt);
        }
        else {
            return defaultValue;
        }
    }
    
   /**
     * Returns the users input of the type int in the case of update.
     * 
     * @param prompt A text to be displayed for the user that can be parametrized in the method call
     * from the Menu class.
     * @param defaultValue The old value of the int.
     * 
     * @return defaultValue If the user does not want to change the old value.
     * OR 
     * inputInt If the user wants to change the old value.
     */
    public int updateInt(String prompt, int defaultValue){
        if (inputBoolean("Vil du ændre " + prompt + "?")){
            return inputInt("Ny værdi for " + prompt);
        }
        else {
            return defaultValue;
        }
    }
    
    /**
     * Returns the users input of the type double in the case of update.
     * 
     * @param prompt A text to be displayed for the user that can be parametrized in the method call
     * from the Menu class.
     * @param defaultValue The old value of the double.
     * 
     * @return defaultValue If the user does not want to change the old value.
     * OR 
     * inputDouble If the user wants to change the old value.
     */
    public double updateDouble(String prompt, double defaultValue){
        if (inputBoolean("Vil du ændre " + prompt + "?")){
            return inputDouble("Ny værdi for " + prompt);
        }
        else {
            return defaultValue;
        }
    }

}
